package com.xinghou.platform.lucene.main.util;


import com.xinghou.platform.util.StringUtils;

/**
 * Created by qingshengzheng on 16/6/27.
 */
public class SolrQuery {
    public static String OPER_AND = " AND ";
    public static String OPER_OR = " OR ";
    public static String OPER_IN = " IN ";
    public StringBuffer querydesc = new StringBuffer();

    public static SolrQuery instance() {
        return new SolrQuery();
    }

    /**
     * equal操作
     *
     * @param operator
     * @param v
     * @return
     */
    public SolrQuery equal(String operator, String field, String v) {
        if (StringUtils.isEmpty(v))
            return this;
        if (!StringUtils.isEmpty(operator)&&!StringUtils.isEmpty(querydesc.toString()))
            querydesc.append(operator);
        querydesc.append(field).append(":").append(v);
        return this;
    }

    public SolrQuery equal(String field, String v) {
        return equal(null, field, v);
    }


    /**
     * @param operator
     * @param field
     * @param v
     * @return
     */
    public SolrQuery like(String operator, String field, String v) {
        if (StringUtils.isEmpty(v))
            return this;
        if (!StringUtils.isEmpty(operator)&&!StringUtils.isEmpty(querydesc.toString()))
            querydesc.append(operator);
        querydesc.append(field).append(":").append("*").append(v).append("*");
        return this;
    }


    public SolrQuery in(String operator,String field ,String... v) {
        if (!StringUtils.isEmpty(operator)&&!StringUtils.isEmpty(querydesc.toString()))
            querydesc.append(operator);
        if (field != null)
            querydesc.append("(");
        for (int i = 0; i < v.length; i++) {
            querydesc.append(field).append(":").append(v[i]);
            if (i != v.length - 1)
                querydesc.append(" OR ");
        }
        querydesc.append(")");
        return this;
    }



   /* public SolrQuery in(String operator,String field ,String... v) {
        if (!StringUtils.isEmpty(operator)&&!StringUtils.isEmpty(querydesc.toString()))
            querydesc.append(operator);
        if (field != null)
            querydesc.append(field).append(":(");
        for (int i = 0; i < v.length; i++) {
            querydesc.append(v[i]);
            if (i != v.length - 1)
                querydesc.append(",");
        }
        querydesc.append(")");
        return this;
    }*/


    public SolrQuery like(String field, String v) {
        return like(null, field, v);
    }

    public SolrQuery between(String field, String min, String max) {
        if (StringUtils.isEmpty(min)) min = "0";
        if (StringUtils.isEmpty(max)) max = Integer.MAX_VALUE + "";
        querydesc.append(" " + field + ":[" + min + " TO " + max + "] ");
        return this;
    }

    public SolrQuery between(String oper, String field, String min, String max) {
        if (!StringUtils.isEmpty(oper)&&!StringUtils.isEmpty(querydesc.toString()))
        querydesc.append(oper + " ");
        between(field, min, max);
        return this;
    }


    public String toString() {

        if (StringUtils.isEmpty(querydesc.toString()))
            return "*:*";
        return querydesc.toString();
    }


}
